use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::LinkedList;
use std::collections::BinaryHeap;
use std::collections::VecDeque;

// 1. vec
fn vec_example() {
    let mut v = Vec::new();
    v.push(1);
    v.push(2);
    v.push(3);
    v.push(4);
    v.push(5);
    println!("{:?}", v);

    v.pop();
    println!("{:?}", v);

    v.remove(2);
    println!("{:?}", v);

    v.insert(2, 100);
    println!("{:?}", v);

    v.clear();
    println!("{:?}", v);

    let mut v2 = vec![1.1, 2.2, 3.3, 4.4, 5.5];
    println!("{:?}", v2);

    let vv = v2[2];
    println!("{}", vv);

    let vv1:Option<&f64> = v2.get(1);
    if let Some(v) = vv1 {
        println!("{}", v);
    }

    let vv2:Option<&f64> = v2.get(10);
    match vv2 {
        Some(v) => println!("{}", v),
        None => println!("None"),        
    }

    for i in &v2 {
        print!("{} ", i);
    }
    println!();

    for i in v2.iter() {
        print!("{} ", i);
    }
    println!();

    // 在使用 *= 运算符之前必须使用解引用运算符（*）获取 i 中的值，然后对其进行修改
    for i in &mut v2 {
        *i *= 2.0;
    }
    println!("{:?}", v2);

}

// 2. HashMap
fn hashmap_example() {
    let mut m = HashMap::new();
    m.insert("a", 1);
    m.insert("b", 2);
    m.insert("c", 3);
    m.insert("d", 4);
    m.insert("e", 5);
    println!("{:?}", m);

    let v = m.get("b");
    if let Some(v) = v {
        println!("{}", v);
    }

    let v1 = m.get("f");
    match v1 {
        Some(v) => println!("{} ", v),
        None => println!("None"),
    }

    for (k, v) in &m {
        print!("{}: {},", k, v);
    }
    println!();

    for (k, v) in m.iter() {
        print!("{}: {},", k, v);
    }
    println!();

    for (k, v) in m.iter_mut() {
        *v *= 2;
        print!("{}: {},", k, v);
    }
    println!();

    m.remove("e");
    println!("{:?}", m);

    m.clear();
    println!("{:?}", m);
}

// 3. HashSet
fn hashset_example() {
    let mut s = HashSet::new();
    s.insert(1);
    s.insert(3);
    s.insert(5);
    s.insert(4);
    s.insert(2);
    println!("{:?}", s);

    s.remove(&3);
    println!("{:?}", s);

    s.take(&5);
    println!("{:?}", s);

    s.clear();
    println!("{:?}", s);

}

// 4. BTreeMap
fn btree_map_example() {
    let mut bm = BTreeMap::new();
    bm.insert(1, "a");
    bm.insert(5, "b");
    bm.insert(2, "c");
    bm.insert(4, "d");
    bm.insert(3, "e");
    println!("{:?}", bm);

    let v = bm.get(&2);
    if let Some(v) = v {
        println!("{}", v);
    }

    let v1 = bm.get(&6);
    match v1 {
        Some(v) => println!("{} ", v),
        None => println!("None"),
    }

    for (k, v) in &bm {
        print!("{}: {},", k, v);
    }
    println!();

    for (k, v) in bm.iter() {
        print!("{}: {},", k, v);
    }
    println!();

    for (k, v) in bm.iter_mut() {
        *v = "x";
    }
    println!( "{:?}", bm);

    bm.remove(&4);
    println!("{:?}", bm);

    bm.clear();
    println!("{:?}", bm);
}

// 5. BTreeSet
fn btree_set_example() {
    let mut bs = BTreeSet::new();
    bs.insert(1);
    bs.insert(3);
    bs.insert(5);
    bs.insert(4);
    bs.insert(2);
    println!("{:?}", bs);

    bs.remove(&3);
    println!("{:?}", bs);

    bs.take(&5);
    println!("{:?}", bs);

    bs.clear();
    println!("{:?}", bs);
}

// 6. LinkedList
fn linked_list_example() {
    let mut ll = LinkedList::new();
    ll.push_back(1);
    ll.push_back(2);
    ll.push_back(3);
    ll.push_back(4);
    ll.push_back(5);
    println!("{:?}", ll);
    ll.push_front(100);
    println!("{:?}", ll);

    let index = 10;
    if let Some(v) = ll.iter().nth(index) {
        println!("{}", v);
    } else {
        println!("None");
    }

    for i in &ll {
        print!("{} ", i);
    }
    println!();

    for i in ll.iter() {
        print!("{} ", i);
    }
    println!();

    for i in &mut ll {
        *i *= 2;
    }
    println!("{:?}", ll);

    ll.pop_front();
    println!("{:?}", ll);

    ll.pop_back();
    println!("{:?}", ll);

    ll.clear();
    println!("{:?}", ll);
}

// 7. BinaryHeap
fn binary_heap_example() {
    let mut bh = BinaryHeap::new();
    bh.push(1);
    bh.push(5);
    bh.push(2);
    bh.push(4);
    bh.push(3);
    println!("{:?}", bh);

    for i in &bh {
        print!("{} ", i);
    }
    println!();

    for i in bh.iter() {
        print!("{} ", i);
    }
    println!();

    let v = bh.peek();
    if let Some(v) = v {
        println!("{}", v);
    }

    let mut bh1 = bh.clone();
    let bh2 = bh.clone();

    // 使用 pop 方法遍历堆-> 从大到小排序
    while let Some(item) = bh.pop() {
            print!("{} ", item);
    }
    println!("{}",bh.is_empty());

    // 使用 drain 方法遍历并清空堆-> 没有排序
    for item in bh1.drain() {
        print!("{} ", item);
    }
    println!("{}",bh1.is_empty());

    bh2.into_iter().for_each(|item| {
        print!("{} ", item);
    });
    println!();


    // for i in &mut bh {
    //     *i *= 2;
    // }
    // println!("{:?}", bh);

    bh.clear();
    println!("{:?}", bh);
}

// 8. VecDeque
fn vec_deque_example() {
    let mut vd = VecDeque::new();
    vd.push_back(1);
    vd.push_back(5);
    vd.push_back(2);
    vd.push_back(4);
    vd.push_back(3);
    println!("{:?}", vd);

    let v = vd.get(2);
    if let Some(v) = v {
        println!("{}", v);
    }

    let v1 = vd.get(10);
    match v1 {
        Some(v) => println!("{} ", v),
        None => println!("None"),
    }

    for i in &vd {
        print!("{} ", i);
    }
    println!();

    for i in vd.iter() {
        print!("{} ", i);
    }
    println!();

    for i in &mut vd {
        *i *= 2;
    }
    println!("{:?}", vd);

    vd.pop_front();
    println!("{:?}", vd);

    vd.pop_back();
    println!("{:?}", vd);

    vd.clear();
    println!("{:?}", vd);
}

// 9. SmallVec
fn small_vec_example() {
    let mut sv = smallvec::SmallVec::<[u8; 16]>::new();
    sv.push(1);
    sv.push(2);
    sv.push(3);
    sv.push(4);
    sv.push(5);
    println!("{:?}", sv);

    sv.pop();
    println!("{:?}", sv);

    sv.remove(2);
    println!("{:?}", sv);

    sv.insert(2, 100);
    println!("{:?}", sv);

    sv.clear();
    println!("{:?}", sv);
}

// 10. String
fn string_example() {    
    let mut s = String::from("你好,");
    s.push_str(" 世界");
    s.push('!');
    println!("{}", s);

    let s1 = s.clone();
    println!("{}", s1);

    let s2 = s.split_whitespace().collect::<Vec<&str>>();
    println!("{:?}", s2);

    let s3 = s.replace("世界", "rust");
    println!("{}", s3);

    let s4 = s.chars().nth(1).unwrap();
    println!("{}", s4);

    let s5 = s.chars().count();
    println!("{}", s5);
}

fn main() {
    vec_example();
    println!("----------------------vec----------------------------------");
    string_example();
    println!("----------------------string----------------------------");
    hashmap_example();
    println!("------------------------hasmap-----------------------------");
    hashset_example();
    println!("------------------------hashset-----------------------------");
    btree_map_example();
    println!("-------------------------btreemap----------------------------");
    btree_set_example();
    println!("-------------------------btreeset----------------------------");
    linked_list_example();
    println!("-------------------------linkedlist-------------------------");
    binary_heap_example();
    println!("-------------------------binaryheap-------------------------");
    vec_deque_example();
    println!("-------------------------vecdeque----------------------------");
    small_vec_example();
    println!("-------------------------smallvec----------------------------");
    
}
